<?php
/*
 * testInjector.php created on 4 mai 08 
 * Author: Rodrigue Le Gall
 *
 * RPortal is an open source web based Content Management System
 * Copyright (C) 2002-2008  Rodrigue Le Gall
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

require_once 'PHPUnit/Framework.php';
require_once 'rportal/private/Injector.php';
require_once 'rportal/private/RPFileNotFoundException.php';
require_once 'rportal/private/RPUndefinedClassException.php';

require_once dirname(__FILE__).'/Fixtures/MockClass.php';
require_once dirname(__FILE__).'/Fixtures/MockClass2.php';

 /**
  * Tests for the implementation of dependency injection
  */
class InjectorTest extends PHPUnit_Framework_TestCase {
	
	public function setUp(){
		Injector :: $dbhandle = sqlite_factory(dirname(__FILE__) . '/Fixtures/testInjector.db');
		Injector :: $dbhandle->query("PRAGMA synchronous=OFF");
		Injector::initDB();
	}
	
	public function testAddClass(){
		$this->assertTrue(Injector::addClass('MockClass',dirname(__FILE__).'/Fixtures/MockClass.php'));
	}
	
	public function testAddInjection(){
		try{
			Injector::addClass('MockClass',dirname(__FILE__).'/Fixtures/MockClass.php');
			$this->assertTrue(Injector::addInjection('myKey','MockClass'));
		}catch(Exception $e){
			$this->fail('Exception should not be raised');
		}
	}
	
	public function testAddInjectionNotDefined(){
		try{
			$this->assertFalse(Injector::addInjection('myKey','myClass'));
		}catch(RPUndefinedClassException $e){
			return;
		}
		$this->fail('RPUndefinedClassException should be raised');
	}
	
	public function testGetClassByKey(){
		Injector::addClass('MockClass',dirname(__FILE__).'/Fixtures/MockClass.php');
		Injector::addInjection('myKey','MockClass');
		$this->assertEquals('MockClass',Injector::getClass('myKey'));
	}
	
	public function testGetClass(){
		Injector::addClass('MockClass',dirname(__FILE__).'/Fixtures/MockClass.php');
		$this->assertEquals('MockClass',Injector::getClass('MockClass'));
	}
	
	public function testUndefinedGetClass(){
		try{
			Injector::getClass('myKey');
		}catch(RPUndefinedClassException $e){
			return;
		}
		$this->fail('RPUndefinedClassException should be raised');
	}
	
	public function testGetObject(){
		Injector::addClass('MockClass',dirname(__FILE__).'/Fixtures/MockClass.php');
		Injector::addInjection('myKey','MockClass');
		$this->assertType('MockClass',Injector::getObject('myKey'));
	}
	
	public function testGetObjectWithParam(){
		Injector::addClass('MockClass2',dirname(__FILE__).'/Fixtures/MockClass2.php');
		Injector::addInjection('myKey','MockClass2');
		$this->assertAttributeEquals('test','name',Injector::getObject('myKey','test'));
	}
	
	public function testDelClass(){
		Injector::addClass('MockClass',dirname(__FILE__).'/Fixtures/MockClass.php');
		$this->assertTrue(Injector::delClass('MockClass'));
	} 
	
	public function testDelInjection(){
		Injector::addClass('MockClass',dirname(__FILE__).'/Fixtures/MockClass.php');
		Injector::addInjection('myKey','MockClass');
		$this->assertTrue(Injector::delInjection('myKey'));
		try{
			Injector::getClass('myKey');
		}catch(RPUndefinedClassException $e){
			return;
		}
		$this->fail('RPUndefinedClassException should be raised');
	}
	
	public function testGetFile(){
		Injector::addClass('MockClass',dirname(__FILE__).'/Fixtures/MockClass.php');
		$this->assertEquals(dirname(__FILE__).'/Fixtures/MockClass.php',Injector::getFile('MockClass'));
	}
	
}